package dto

import (
	"sort"
	"strconv"
	"strings"
	"time"
	"vrcm/src/vrcm/app/admin/models"
	"vrcm/src/vrcm/app/admin/models/subject"
	"vrcm/src/vrcm/common/dto"
	common "vrcm/src/vrcm/common/models"
)

type SubjectGetPageReq struct {
	dto.Pagination `search:"-"`
	EventId        string `json:"event_id"`
	TenantId       string `json:"tenant_id"`
	PaperId        int    `json:"paper_id" validate:"required"`
	IsDelete       int    `json:"is_delete"  search:"type:exact;column:is_delete;table:subject"`
	//SubjectOrder
}

type SubjectOrder struct {
	Id           string `form:"id"  search:"type:order;column:id;table:subject"`
	SubjectName  string `form:"subjectNameOrder"  search:"type:order;column:subject_name;table:subject"`
	SubjectTitle string `form:"subjectTitleOrder"  search:"type:order;column:subject_title;table:subject"`
	//EventId      string `form:"event_id"  search:"type:order;column:event_id;table:subject"`
	TenantId   string `form:"tenant_id"  search:"type:order;column:tenant_id;table:subject"`
	ChooseType string `form:"chooseTypeOrder"  search:"type:order;column:choose_type;table:subject"`
	ExpiryTime string `form:"expiryTimeOrder"  search:"type:order;column:expiry_time;table:subject"`
	Option     string `form:"optionOrder"  search:"type:order;column:option;table:subject"`
	Answer     string `form:"answerOrder"  search:"type:order;column:answer;table:subject"`
	CreateTime string `form:"createTimeOrder"  search:"type:order;column:create_time;table:subject"`
	UpdateTime string `form:"updateTimeOrder"  search:"type:order;column:update_time;table:subject"`
	CreateBy   string `form:"createByOrder"  search:"type:order;column:create_by;table:subject"`
	UpdateBy   string `form:"updateByOrder"  search:"type:order;column:update_by;table:subject"`
	IsDelete   int    `form:"is_delete"  search:"type:exact;column:is_delete;table:subject"`
}

func (m *SubjectGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type SubjectInsertReq struct {
	Id           int    `json:"-" comment:"id"` // id
	SubjectName  string `json:"subject_name" comment:"题目名称" validate:"required,max=100"`
	SubjectTitle string `json:"subject_title" comment:"题干" validate:"required,max=255"`
	TitleImg     string `json:"title_img" comment:"题目图片"`
	//EventId      string `json:"event_id" comment:"活动id" validate:"required"`
	//TenantId    string `json:"tenant_id" comment:"租户id" validate:"required"`
	PaperId     int `json:"paper_id" comment:"试卷id" validate:"required,gt=0"`
	ChooseType  int `json:"choose_type" comment:"1.单选 2.多选" validate:"required,gt=0,lte=2"`
	SubjectType int `json:"subject_type" comment:"题目类型 1.不含图片 2含图片" validate:"required,gt=0,lte=2"`
	ExpiryTime  int `json:"expiry_time" comment:"题目时长" validate:"required,gt=0"`
	//Options      []string `json:"options" comment:"选项" validate:"required,max=500"`
	Answers      []string                `json:"answers" comment:"答案" validate:"required,max=200"`
	MaxChooseNum int                     `json:"max_choose_num" comment:"最大选择个数" validate:"gt=0"`
	Options      []subject.SubjectOption `json:"options" comment:"选项" validate:"required"`
	common.ControlBy
}

func (s *SubjectInsertReq) Generate(model *subject.Subject) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.CreateTime = time.Now().Unix()
	model.UpdateTime = time.Now().Unix()
	model.SubjectName = s.SubjectName
	model.SubjectTitle = s.SubjectTitle
	model.TitleImg = s.TitleImg
	//model.EventId = s.EventId
	//model.TenantId = s.TenantId
	model.PaperId = s.PaperId
	model.ChooseType = s.ChooseType
	model.SubjectType = s.SubjectType
	model.ExpiryTime = s.ExpiryTime
	model.MaxChooseNum = s.MaxChooseNum
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
	model.IsDelete = 2
	if len(s.Answers) > 0 {
		//答案做排序处理
		sort.Slice(s.Answers, func(i, j int) bool {
			n1, _ := strconv.Atoi(s.Answers[i])
			n2, _ := strconv.Atoi(s.Answers[j])
			return n1 < n2
		})
		tempAnswers := strings.Join(s.Answers, models.Delimiter)
		model.Answer = tempAnswers
	}
}

func (s *SubjectInsertReq) Generate2(options *[]subject.SubjectOption) {

	if len(s.Options) > 0 {
		for k, v := range s.Options {
			subjectOption := subject.SubjectOption{}
			subjectOption.SubjectId = s.Id
			subjectOption.OptionImage = v.OptionImage
			subjectOption.OptionContent = v.OptionContent
			subjectOption.OptionType = v.OptionType
			subjectOption.Sort = k
			subjectOption.IsDelete = 2
			subjectOption.CreateTime = time.Now().Unix()
			subjectOption.UpdateTime = time.Now().Unix()
			subjectOption.CreateBy = v.CreateBy
			*options = append(*options, subjectOption)
		}
	}
}
func (s *SubjectInsertReq) GetId() interface{} {
	return s.Id
}

type SubjectUpdateReq struct {
	Id           int    `uri:"id" comment:"id" validate:"required"` // id
	SubjectName  string `json:"subject_name" comment:"题目名称" validate:"required,max=100"`
	SubjectTitle string `json:"subject_title" comment:"题干" validate:"required,max=255"`
	TitleImg     string `json:"title_img" comment:"题目图片" validate:"max=300"`
	//EventId      string `json:"event_id" comment:"活动id" validate:"required"`
	//TenantId    string `json:"tenant_id" comment:"租户id" validate:"required"`
	PaperId     int `json:"paper_id" comment:"试卷id" validate:"required,gt=0"`
	ChooseType  int `json:"choose_type" comment:"1.单选 2.多选" validate:"required,gt=0,lte=2"`
	SubjectType int `json:"subject_type" comment:"题目类型 1.不含图片 2含图片" validate:"required,gt=0,lte=2"`
	ExpiryTime  int `json:"expiry_time" comment:"题目时长(秒)" validate:"required,gt=0"`
	//Options      []string `json:"options" comment:"选项" validate:"required,max=500"`
	Answers      []string                `json:"answers" comment:"答案" validate:"required,max=50"`
	MaxChooseNum int                     `json:"max_choose_num" comment:"最大选择个数" validate:"gt=0"`
	Options      []subject.SubjectOption `json:"options" comment:"选项" validate:"required"`
	common.ControlBy
}

func (s *SubjectUpdateReq) Generate(model *subject.Subject, options *[]subject.SubjectOption) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.SubjectName = s.SubjectName
	model.SubjectTitle = s.SubjectTitle
	model.TitleImg = s.TitleImg
	//model.EventId = s.EventId
	//model.TenantId = s.TenantId
	model.ChooseType = s.ChooseType
	model.SubjectType = s.SubjectType
	model.ExpiryTime = s.ExpiryTime
	model.UpdateTime = time.Now().Unix()
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
	model.IsDelete = 2
	model.MaxChooseNum = s.MaxChooseNum
	model.PaperId = s.PaperId
	if len(s.Answers) > 0 {
		tempAnswers := strings.Join(s.Answers, models.Delimiter)
		model.Answer = tempAnswers
	}
	if len(s.Options) > 0 {
		for _, v := range s.Options {
			subjectOption := subject.SubjectOption{}
			subjectOption.Model = common.Model{Id: v.Id}
			subjectOption.SubjectId = s.Id
			subjectOption.OptionImage = v.OptionImage
			subjectOption.OptionContent = v.OptionContent
			subjectOption.OptionType = v.OptionType
			subjectOption.Sort = v.Sort
			subjectOption.CreateTime = time.Now().Unix()
			subjectOption.UpdateTime = time.Now().Unix()
			subjectOption.CreateBy = v.CreateBy
			subjectOption.UpdateBy = v.UpdateBy
			*options = append(*options, subjectOption)
		}
	}
}

func (s *SubjectUpdateReq) GetId() interface{} {
	return s.Id
}

// SubjectGetReq 功能获取请求参数
type SubjectGetReq struct {
	Id int `uri:"id" validate:"required"`
}

type SubjectGetByPaperIdReq struct {
	PaperIds []int `json:"paper_ids" comment:"试卷id" validate:"required"`
}

func (s *SubjectGetReq) GetId() interface{} {
	return s.Id
}

// SubjectDeleteReq 功能删除请求参数
type SubjectDeleteReq struct {
	Ids []int `json:"ids" validate:"required"`
}

func (s *SubjectDeleteReq) GetId() interface{} {
	return s.Ids
}
